//
// definition of loading scene
//
var picoSE   = null;
var titleBGM = null;

var LoadingScene = {
    scene:null,
    kawazLogo:null,
    loadingCount:0,
    progressBar:null,
    loaded:null,
    started:null,
    logoTransform:null,
    sceneTransform:null,
    logoCompleted:null,
    init:function() {
        this.scene = quicktigame2d.createScene();
        
        var loadingTextures = [
            "graphics/title/title_background.png",
            "graphics/title/logo.png",
            "graphics/title/button_start.png",
            "graphics/title/button_credit.png",
            "graphics/title/button_howto.png"
        ];
        
        var onSceneTransformComplete = (function(self) {
            return function() {
	        	self.started = true;

				// Start fade in logo
	        	self.logoTransform.duration = 1000;
	        	self.logoTransform.alpha    = 1;
	        	self.logoTransform.easing   = quicktigame2d.ANIMATION_CURVE_CUBIC_IN;
				self.kawazLogo.transform(self.logoTransform);
            };
        })(this);

        var onLogoTransformComplete = (function(self) {
            return function() {
            	self.logoCompleted = true;
            };
        })(this);

        var onloadsprite = (function(self) {
            return function(e) {
                Ti.API.info("onloadsprite: " + e.name);
                if (e.name == "graphics/title/kawaz.png") {
					// Start fade in background black to white
					self.sceneTransform.duration = 500;
					self.sceneTransform.color(1, 1, 1);
					self.scene.transform(self.sceneTransform);
                }
            };
        })(this);
        
        var enterframe = (function(self) {
            return function(e) {
                if (self.loaded) return;
                if (!self.started) return;
                if (self.loadingCount >= loadingTextures.length) {
                	
                	if (!self.logoCompleted) return;
                	
                	titleBGM = Ti.Media.createSound({url:'/music/title.wav'});
                	picoSE  = Ti.Media.createSound({url:'/se/pico.wav'});
                
                    self.loaded = true;
                    self.scene.removeEventListener('enterframe',  enterframe);
                
                    TitleScene.init();
                    HowtoScene.init();
                    CreditScene.init();
                    MainLoadingScene.init();
                    MainScene.init();
                
                    game.replaceScene(TitleScene.scene);
                    return;
                }
                if (self.progressBar != null) {
                    self.progressBar.value = self.loadingCount + 1;
                    self.progressBar.message = "Loading " + self.progressBar.value + " of " + loadingTextures.length;
                }
                game.loadTexture(loadingTextures[self.loadingCount]);
                self.loadingCount++;
            };
        })(this);
        
        var startloading = (function(self) {
            return function(e) {
                self.scene.addEventListener('enterframe',  enterframe);
                window.add(self.progressBar);
                self.progressBar.show();
            }
        })(this);
        
        var activated = (function(self) {
            return function(e) {
                Ti.API.info("loading scene is activated");
            
        		self.loadingCount = 0;
        		self.loaded  = false;
        		self.started = false;
        		self.logoCompleted = false;

				// initial background color equals black       
                self.scene.color(0, 0, 0);
                
                if (self.progreeBar == null) {
                    self.progressBar =Titanium.UI.createProgressBar({
                        width:game.width * 0.5,
                        height:70,
                        min:0,
                        max:loadingTextures.length,
                        value:0,
                        color:'#888',
                        message:'Loading...',
                        font:{fontSize:14, fontWeight:'bold'},
                        style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN,
                        top:game.height * 0.5  
                    });
                }

                // Create sprite instance    
                if (self.kawazLogo == null) {
                    self.kawazLogo = quicktigame2d.createSprite({image:'graphics/title/kawaz.png'});
                }
                
                if (self.logoTransform == null) {
                	self.logoTransform = quicktigame2d.createTransform();
					self.logoTransform.addEventListener('complete', onLogoTransformComplete);
                }
                if (self.sceneTransform == null) {
                	self.sceneTransform = quicktigame2d.createTransform();
					self.sceneTransform.addEventListener('complete', onSceneTransformComplete);
                }
    
                // Move logo at center of the screen
                self.kawazLogo.x = (game.screen.width  * 0.5) - (self.kawazLogo.width  * 0.5);
                self.kawazLogo.y = (game.screen.height * 0.5) - (self.kawazLogo.height * 0.5);
                self.kawazLogo.hide();

                // Add logo to the scene
                self.scene.add(self.kawazLogo);
                    
                setTimeout(startloading, 2500);
            };
        })(this);
        
        var deactivated = (function(self) {
            return function(e) {
                Ti.API.info("loading scene is deactivated");
            
                // remove logo from the scene and set variable null
                // that triggers removal from the memory at GC.
                self.scene.remove(self.kawazLogo);
                self.kawazLogo = null;
                
                // If you would like to remove cached texture resource from the memory,
                // use GameView.unloadTexture(texture_name) that unloads texture resource with given name.
                game.unloadTexture('graphics/title/kawaz.png');
                
                window.remove(self.progressBar);
                self.progressBar.hide();
                self.progressBar = null;

                self.logoTransform  = null;
                self.sceneTransform = null;
            };
        })(this);
        
        // register scene activationi event
        this.scene.addEventListener('activated',   activated);
        this.scene.addEventListener('deactivated', deactivated);
        this.scene.addEventListener('onloadsprite',  onloadsprite);
    },
};
